package com.sunshine.asm.oneday;

import java.io.IOException;

import org.objectweb.asm.ClassAdapter;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;

public class HelloClassLoader extends ClassLoader {

	public Class defineClass(String name, byte[] bytes) {
		return defineClass(name, bytes, 0, bytes.length);
	}

	@Override
	protected Class<?> findClass(String name) throws ClassNotFoundException {
		//
		Class cls = super.findClass(name);
		if (cls == null)
			return cls;

		try {
			ClassReader cr = new ClassReader(name);
			
			
			ClassWriter cw = new ClassWriter(0);
			//transform class
			ClassAdapter ca  =new ClassAdapter(cw);
			cr.accept(ca, 0);
			
			return defineClass(name, cw.toByteArray());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}
}
